﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Criterion;

namespace Biza.Core.Orm
{
    [Serializable]
    public class Repository
    {
        private NHibernate.ISession m_session;
        public Repository(NHibernate.ISession session)
        {
            this.m_session = session;
        }

        public T Get<T>(object id)
        {
            return this.m_session.Get<T>(id);
        }
        public IList<T> GetAll<T>()
        {
            return this.m_session.CreateCriteria(typeof(T)).List<T>();
        }

        public IList<T> Query<T>(DetachedCriteria cri)
        {
            return cri.GetExecutableCriteria(this.m_session).List<T>();
        }
        public IList<T> Query<T>(Func<T, bool> predicate)
        {
            return this.m_session.Query<T>().Where(predicate).ToList<T>();
        }
        public IQueryable<T> Query<T>()
        {
            return m_session.Query<T>();
        }

        public void Update<T>(T value)
        {
            this.m_session.Update(value);
            this.m_session.Flush();
        }
        public void SaveOrUpdate<T>(T value)
        {
            this.m_session.SaveOrUpdate(value);
            this.m_session.Flush();
        }
        public void Save<T>(T value)
        {
            this.m_session.Save(value);
            this.m_session.Flush();
        }
        public void Delete<T>(T value)
        {
            this.m_session.Delete(value);
        }
    }
}
